package com.lening.common.base;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @author 牛强
 * @datetime 2024/4/16 8:39
 * @description 类对象
 */
public class BaseController<P, D, S extends BaseService<P, D>> {
    @Autowired(required = false)
    private S service;

    @PostMapping("add")
    @ApiOperation("添加")
    public String add(@RequestBody P p) {
        return service.add(p);
    }

    @PostMapping("adds")
    @ApiOperation("批量添加")
    public String adds(@RequestBody Collection<P> ps) {
        return service.adds(ps);
    }

    @PutMapping("update")
    @ApiOperation("修改")
    public String update(@RequestBody P p) {
        return service.update(p);
    }

    @PutMapping("updates")
    @ApiOperation("批量修改")
    public String updates(@RequestBody Collection<P> ps) {
        return service.updates(ps);
    }

    @RequestMapping(value="addOrUpdate",method = {RequestMethod.PUT,RequestMethod.POST})
    @ApiOperation("添加或修改")
    public String addOrUpdate(@RequestBody P p) {
        return service.addOrUpdate(p);
    }

    @DeleteMapping("delById/{id}")
    @ApiOperation("删除")
    public String delById(@PathVariable("id") Long id) {
        return service.delById(id);
    }

    @DeleteMapping("delByIds")
    @ApiOperation("批量删除")
    public String delByIds(@RequestParam("ids") Collection<Long> ids) {
        return service.delByIds(ids);
    }

    @GetMapping("getById/{id}")
    @ApiOperation("根据id查询")
    public P getById(@PathVariable("id") Long id) {
        return service.getById(id);
    }

    @GetMapping("getByIds")
    @ApiOperation("根据id批量查询")
    public List<P> getByIds(@RequestParam("ids") Collection<Long> ids) {
        return service.getByIds(ids);
    }

    @GetMapping("listByMap")
    @ApiOperation("根据Map查询")
    public List<P> listByMap(@RequestParam Map<String, Object> param) {
        return service.listByMap(param);
    }

    @GetMapping("list")
    @ApiOperation("查询所有")
    public List<?> list(D d) {
        return service.list(d);
    }

    @GetMapping("page")
    @ApiOperation("分页查询")
    public Page<?> page(D d) {
        return service.page(d);
    }

    @PutMapping("enable")
    @ApiOperation("启用")
    public String enable(@RequestParam("id") Long id) {
        return service.enable(id);
    }

    @PutMapping("unable")
    @ApiOperation("禁用")
    public String unable(@RequestParam("id") Long id) {
        return service.unable(id);
    }
    @PutMapping("verify")
    public String verify(@RequestParam("id") Long id) {
        return service.verify(id);
    }
    @PutMapping("unverify")
    public String unverify(@RequestParam("id") Long id) {
        return service.unverify(id);
    }
}
